package servlet;

import db.DB;
import model.purchase_list_goods;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/editplistgood")//访问的时候给一个地址,高于3.0版本的包才能这么配地址
public class editplistgood extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setHeader("content-type","text/html;charset=utf-8");
        int plistid=Integer.parseInt(req.getParameter("listid"));
        int goodid=Integer.parseInt(req.getParameter("goodid"));
        int amount=Integer.parseInt(req.getParameter("goodamount"));


        purchase_list_goods plg=new purchase_list_goods();
        plg.setPurchase_List_id(plistid);
        plg.setGoods_id(goodid);
        plg.setPurchase_quantity(amount);

        HttpSession session=req.getSession();

        int num = (new DB().checkpurchasestock(plistid,goodid)+amount);
//        System.out.println(amount);
//        System.out.println(new DB().checkpurchasestock(plistid,goodid));
//        System.out.println(num);
        if(num>=0){
            new DB().editplistgood(plg);//更新商品订单
            new DB().editgoodstock(goodid,num);//更新库存
            new DB().updateplist(plistid);//更新订单价格
        }
        else{
            String message = "修改失败！可能原因：商品库存不足";
            session.setAttribute("message",message);
        }
        if(Integer.parseInt(req.getParameter("whichpage"))==2){
            session.setAttribute("buypage","showlist2");
        }
        else{
            session.setAttribute("buypage","createlist2");
        }
        resp.sendRedirect("buysystem.jsp");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp); }
}

